package com.sooncode.project.focusservice.model;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.alibaba.excel.enums.BooleanEnum;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import com.alibaba.excel.enums.poi.VerticalAlignmentEnum;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy;
import lombok.Data;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.springframework.util.CollectionUtils;
import org.springframework.util.PropertyPlaceholderHelper;


import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@Data
@HeadRowHeight(35)
@ContentRowHeight(110)
@ColumnWidth(20)
@ContentStyle(wrapped = BooleanEnum.TRUE)
public class TimetableExport extends AbstractVerticalCellStyleStrategy implements CellWriteHandler {
    @ExcelProperty(value = {"专注力训练课程表", "姓名:${name}", "课程批次"}, index = 0)
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    @ColumnWidth(16)
    private String batch;
    @ExcelProperty(value = {"专注力训练课程表", "姓名:${name}", "听觉训练"}, index = 1)
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.LEFT,
            verticalAlignment = VerticalAlignmentEnum.TOP,
            wrapped = BooleanEnum.TRUE
    )
    private String hearing1;
    @ExcelProperty(value = {"专注力训练课程表", "性别:${sex}", "听觉训练"}, index = 2)
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.LEFT, verticalAlignment = VerticalAlignmentEnum.TOP, wrapped = BooleanEnum.TRUE)
    private String hearing2;
    @ExcelProperty(value = {"专注力训练课程表", "性别:${sex}", "视觉训练"}, index = 3)
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.LEFT, verticalAlignment = VerticalAlignmentEnum.TOP, wrapped = BooleanEnum.TRUE)
    private String vision1;
    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.LEFT, verticalAlignment = VerticalAlignmentEnum.TOP, wrapped = BooleanEnum.TRUE)
    @ExcelProperty(value = {"专注力训练课程表", "年龄:${age}", "视觉训练"}, index = 4)
    private String vision2;

    @ExcelIgnore
    private String name;
    @ExcelIgnore
    private String sex;
    @ExcelIgnore
    private String age;
    @ExcelIgnore
    PropertyPlaceholderHelper placeholderHelper = new PropertyPlaceholderHelper("${", "}");

    public TimetableExport() {
    }

    public TimetableExport(String name, String sex, String age) {
        setName(name);
        setSex(sex);
        setAge(age);
    }

    public List<TimetableExport> export(String[][] timetableArr, String phaseName) {
        List<TimetableExport> list = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            TimetableExport timetable = new TimetableExport();
            timetable.setBatch(String.format("%s:第%d节", phaseName, i + 1));
            timetable.setHearing1(handleString(timetableArr[i][0]));
            timetable.setHearing2(handleString(timetableArr[i][1]));
            timetable.setVision1(handleString(timetableArr[i][2]));
            timetable.setVision2(handleString(timetableArr[i][3]));
            list.add(timetable);
        }
        return list;
    }

    private String handleString(String str) {
        if (StringUtils.isEmpty(str)) return str;
        String[] s = str.split("#");
        String[] ts = {s[0], "-------------------------------------"};
        if (s.length > 1)
            ts = ArrayUtils.addAll(ts, s[1].split(","));
        return String.join(String.valueOf((char) 10), ts);
    }

    @Override
    protected WriteCellStyle headCellStyle(CellWriteHandlerContext context) {
        WriteCellStyle writeCellStyle = new WriteCellStyle();
        WriteFont font = new WriteFont();
        font.setFontHeightInPoints((short) 15);
        if (context.getRowIndex() <= 1) {
            writeCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
            writeCellStyle.setBorderTop(BorderStyle.NONE);
            writeCellStyle.setBorderBottom(BorderStyle.NONE);
            writeCellStyle.setBorderLeft(BorderStyle.NONE);
            writeCellStyle.setBorderRight(BorderStyle.NONE);
            font.setBold(false);
            writeCellStyle.setWriteFont(font);
            writeCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        }
        if (context.getRowIndex() == 0) {
            writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
            font.setFontHeightInPoints((short) 27);
            font.setBold(true);
            writeCellStyle.setWriteFont(font);
        }
        return writeCellStyle;
    }
    @Override
    protected WriteCellStyle contentCellStyle(Head head){
        WriteCellStyle writeCellStyle = new WriteCellStyle();
        writeCellStyle.setBorderTop(BorderStyle.THIN);
        writeCellStyle.setBorderBottom(BorderStyle.THIN);
        writeCellStyle.setBorderLeft(BorderStyle.THIN);
        writeCellStyle.setBorderRight(BorderStyle.THIN);
        writeCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        writeCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
        writeCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        writeCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());

        return writeCellStyle;
    }

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
        // 动态设置表头字段
        if (!ObjectUtils.isEmpty(head)) {
            List<String> headNameList = head.getHeadNameList();
            if (!CollectionUtils.isEmpty(headNameList)) {
                Properties properties = new Properties();
                properties.setProperty("name", getName());
                properties.setProperty("sex", getSex());
                properties.setProperty("age", getAge());
                for (int i = 0; i < headNameList.size(); i++) {
                    // 循环遍历替换
                    headNameList.set(i, placeholderHelper.replacePlaceholders(headNameList.get(i), properties));
                }
            }
        }
    }
}
